﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using SGArchiveSystem7xDB;
using SGArchiveSystem7xDB.Linq;

/// <summary>
/// 权限模块
/// </summary>
namespace SGArchiveSystem7x.Controllers.Authority
{
    /// <summary>
    /// 部门权限
    /// </summary>
    public class ArchiveAuthorityController : BaseController
    {
        /// <summary>
        /// 本控制器的名称，用于权限访问 (档案权限)
        /// </summary>
        private string sControllerName = "ArchiveAuthoriy";

        // GET: DepartmentArchiveAuthority
        ///[HttpPost]
        //[ValidateAntiForgeryToken]
        public ActionResult DepartmentArchiveAuthority()
        {
            var n = this.VaildUser(sControllerName);
            if (n != null)
                return n;

            var list = SGArchiveSystem7x.Models.CDepartmentTreeModel.CreateModel();
            
            ViewBag.Tree = list;
            return View();
        }

        /// <summary>
        /// 部门ID
        /// </summary>
        /// <param name="DepartmentId"></param>
        /// <returns></returns>
        ///[HttpPost]
        ///[ValidateAntiForgeryToken]
        public ActionResult DepartmentArchiveList(int DepartmentId,string title)
        {
            //grid 的 headertitle
            ViewBag.Title = title;
            var columns = SGArchiveSystem7x.Models.CColumnsModel.Create_DepartmentArchiveListColumns(true);
            ViewBag.Columns = columns;
            ViewBag.DepId = DepartmentId;
            return View();
        }
        /// <summary>
        /// ajax 获取部门权限列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Get_ajaxDepartmentArchiveList(int DepartmentId)
        {
            var list = SGArchiveSystem7x.Models.CDepartmentTreeModel.CreateModel();
            var listModel = SGArchiveSystem7x.Models.CDepartmentAuthorityModel.CreateModelNew(DepartmentId, list);
            var json = new
            {
                rows = listModel.ToArray(),
                total = listModel.Count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 保存部门权限
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Save_ajaxDepartmentArchive(int departmentid, string data)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            List<Models.CDepartmentAuthorityModel> list = js.Deserialize<List<Models.CDepartmentAuthorityModel>>(data);
            List<T_DepartmentAuthority> listDep = new List<T_DepartmentAuthority>();
            foreach(var n in list)
            {
                T_DepartmentAuthority d = new T_DepartmentAuthority();
                if (n.iEdit > -1)
                {
                    d.DepAut_Approval = n.iApproval;
                    d.DepAut_DID = departmentid;
                    d.DepAut_Down = n.iDown;
                    d.DepAut_DtoID = n.iDepartmentID;
                    d.DepAut_Edit = n.iEdit;
                    d.DepAut_ID = n.ID;
                    d.DepAut_Print = n.iPrint;
                    d.DepAut_Show = n.iShow;
                    d.DepAut_View = n.iView;
                    listDep.Add(d);
                }
            }
            return ((int)T_DeparetmentDal.Set_DepartmentAuthority(departmentid, listDep)).ToString();
        }

        /// <summary>
        /// 根据ID获取部门信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Get_ajaxDepartment(int id)
        {
            var en = T_DeparetmentDal.Get_Department(id);
            var json = new
            {
                en
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 添加部门信息
        /// </summary>
        /// <param name="name"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        [HttpPost]
        public string Add_ajaxDepartment(string name, int status)
        {
            T_Deparetment en = new T_Deparetment()
            {
                Dep_Name = name,
                Dep_PID = 0,
                Dep_Status = status
            };
            var value = (int)T_DeparetmentDal.Add_Department(en);
            return value.ToString();
        }

        /// <summary>
        /// 修改部门名称
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        [HttpPost]
        public string Modify_ajaxDepartment(int id, string name, int status)
        {
            var value = (int)T_DeparetmentDal.Modify_Deparetment(id, name, status);
            return value.ToString();
        }
    }
}